package com.jzo2o.market.controller.consumer;

import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.market.enums.CouponStatusEnum;
import com.jzo2o.market.model.dto.request.SeizeCouponReqDTO;
import com.jzo2o.market.model.dto.response.CouponSimpleInfoResDTO;
import com.jzo2o.market.service.IActivityService;
import com.jzo2o.market.service.ICouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户端 - 优惠卷管理控制器
 * @author JIAN
 */
@Slf4j
@RestController("consumerCouponController")
@RequestMapping("/consumer/coupon")
public class CouponController {
    @Resource
    private IActivityService activityService;
    @Resource
    private ICouponService couponService;

    /**
     * 获取当前用户的优惠卷(滚动查询)
     */
    @GetMapping("/my")
    public List<CouponSimpleInfoResDTO> currentUserCoupon(@RequestParam Integer status,
                                                          @RequestParam(required = false) Integer lastId) {
        CouponStatusEnum couponStatusEnum = CouponStatusEnum.statusOf(status);
        if (ObjectUtils.isEmpty(couponStatusEnum)) {
            throw new BadRequestException("优惠卷状态出错");
        }

        return couponService.getCurrentUserCoupon(couponStatusEnum, lastId);
    }

    /**
     * 用户抢卷接口
     */
    @PostMapping("/seize")
    public void seizeCoupon(@RequestBody SeizeCouponReqDTO seizeCouponReqDTO) {
        activityService.seizeCoupon(seizeCouponReqDTO);
    }
}